﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using Svg;
using System.ComponentModel;
using System.Runtime.Serialization;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using Commons;
//using HumanFace.SketchService;

namespace HumanFace.Common
{
    [DataContract]
    public class Organ
    {
        private float _Width;
        private float _Height;
        private NodeCollection _PointList;
        private Node _MostLeftPoint;
        private Node _MostTopPoint;
        [DataMember]
        public float Width
        {
            get { return _Width; }
            set { _Width = value; }
        }

        [DataMember]
        public float Height
        {
            get { return _Height; }
            set { _Height = value; }
        }
        [DataMember]
        public NodeCollection PointList
        {
            get { return _PointList; }
            set { _PointList = value; }
        }
        [DataMember]
        public Node MostLeftPoint
        {
            get { return _MostLeftPoint; }
            set { _MostLeftPoint = value; }
        }
        [DataMember]
        public Node MostTopPoint
        {
            get { return _MostTopPoint; }
            set { _MostTopPoint = value; }
        }
        /// <summary>
        /// Servisten dışarı çıkması için kullanılır.
        /// </summary>
        [DataMember]
        public string SvgDoc 
        {
            get;
            //{
            //    if (this.SVG == null)
            //        return null;
            //    else
            //    {
            //        using (StringWriter str = new StringWriter())
            //        using (XmlTextWriter xml = new XmlTextWriter(str))
            //        {
            //            this.SVG.WriteElement(xml);
            //            return str.ToString();
            //        }
            //    }
            //}
            set ;
            //{
            //    if (value == null)
            //        this.SVG = null;
            //    else
            //    {
            //        using (StringReader str = new StringReader(value))
            //        using (XmlTextReader xml = new XmlTextReader(str))
            //        {
            //            XmlDocument doc = new XmlDocument();
            //            doc.Load(xml);
            //            this.SVG = SvgDocument.Open(doc);
            //        }
            //    }
            //}
        }

        /// <summary>
        /// Servis içi işlemlerde kullanılır.
        /// </summary>
        [IgnoreDataMember]
        public SvgDocument SVG { get; set; }
        [IgnoreDataMember]
        protected Dictionary<string, string> Templates { get; set; }
        public Organ()
        {
           // RenderVectorelGraphic();

        }
        public Organ(Dictionary<string, string> templates,NodeCollection pointList)
        {
            this.Templates = templates;
            this.PointList = pointList;
            //RenderVectorelGraphic();
        }
        protected virtual void RenderVectorelGraphic()
        {
            if (Templates != null)
            {
                string template = String.Empty;
                this.SvgDoc = Templates.TryGetValue(this.GetType().Name, out template) ? template : null;
            }
            if(this.PointList != null && this.PointList.Count > 0)
            {
                this.MostLeftPoint = this.PointList[0];
                if (this.MostLeftPoint != null)
                {
                    foreach (Node item in this.PointList)
                    {
                        if (item != null && item.X < this.MostLeftPoint.X)
                            this.MostLeftPoint = item;
                    }
                }
                this.MostTopPoint = this.PointList[0];
                if (this.MostTopPoint != null)
                {
                    foreach (Node item in this.PointList)
                    {
                        if (item != null && item.Y < this.MostTopPoint.Y)
                            this.MostTopPoint = item;
                    }
                }
            }
            if (!String.IsNullOrEmpty(this.SvgDoc))
                this.SVG = SketchHelper.ConvertTo(this.SvgDoc);
        }
    }
}
